<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :node-data="company" :root="true" @add-depts="handleAddDepts" />
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 用了一个行列布局 -->
          <template #default="{ data }">
            <TreeTools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="handleAddDepts"
              @edit-depts="editDeptsFn"
            />
          </template>
        </el-tree>

      </el-card>
      <AddDept
        ref="addDept"
        :show-dialog.sync="isShow"
        :dept-list="deptList"
        :node-data="nodeData"
        @add-depts="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import AddDept from './components/add-dept.vue'
import TreeTools from './components/tree-tools.vue'
import { reqGetDepartments } from '@/api/department'
import { transListToTree } from '@/utils'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人'
      },
      deptList: [],
      departs: [],
      isShow: false,
      loading: false,
      nodeData: {},
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const { data: { depts, companyName }} = await reqGetDepartments()
      console.log(depts, companyName)
      this.deptList = depts
      console.log(this.deptList, 111)
      this.company.name = companyName
      this.company.id = ''
      const result = transListToTree(depts, '')
      this.departs = result
      // loading效果
      this.loading = false
    },
    // handleClose(flag) {
    //   this.isShow = flag
    // },
    handleAddDepts(nodeData) {
      this.isShow = true
      console.log(nodeData)
      this.nodeData = nodeData
    },
    editDeptsFn(nodeData) {
      this.isShow = true
      this.nodeData = nodeData
      this.$nextTick(() => {
        this.$refs.addDept.getDepartDetail()
      })
    }
  }

}
</script>

<style lang='scss' scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>

